﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.IO;
using log4net;
using log4net.Config;
using log4net.Core;

namespace ControllerClient
{
    public class LogHelper
    {
        private static ILog log = LogManager.GetLogger("MyLog");

        public static void InitLogConfig(string configFile)
        {
            if (configFile != null)
            {
                try
                {
                    FileInfo configFile2 = new FileInfo(configFile);
                    XmlConfigurator.Configure(configFile2);
                    LogHelper.log = LogManager.GetLogger("MyLog");
                }
                catch
                {

                }
            }
        }

        public static void Debug(string str)
        {
            LogHelper.Debug(str, null, null);
        }

        public static void Debug(string str, Exception ex)
        {
            LogHelper.Debug(str, ex, null);
        }

        public static void Debug(string str, params object[] objs)
        {
            LogHelper.Debug(str, null, objs);
        }

        public static bool IsDebugEnable()
        {
            return LogHelper.log.IsDebugEnabled;
        }

        public static bool IsWarnEnabled()
        {
            return LogHelper.log.IsWarnEnabled;
        }

        public static void Debug(string str, Exception ex, params object[] objs)
        {
            if (LogHelper.log.IsDebugEnabled)
            {
                if (ex == null && objs == null)
                {
                    LogHelper.log.Debug(str);
                }
                else if (objs == null)
                {
                    LogHelper.log.Debug(str, ex);
                }
                else if (ex == null)
                {
                    LogHelper.log.DebugFormat(str, objs);
                }
                else
                {
                    LogHelper.log.DebugFormat(str, objs);
                    LogHelper.log.Debug(ex);
                }
            }
        }

        public static void Info(string str)
        {
            LogHelper.Info(str, null, null);
        }

        public static void Info(string str, Exception ex)
        {
            LogHelper.Info(str, ex, null);
        }

        public static void Info(string str, params object[] objs)
        {
            LogHelper.Info(str, null, objs);
        }

        public static void Info(string str, Exception ex, params object[] objs)
        {
            if (LogHelper.log.IsInfoEnabled)
            {
                if (ex == null && objs == null)
                {
                    LogHelper.log.Info(str);
                }
                else if (objs == null)
                {
                    LogHelper.log.Info(str, ex);
                }
                else if (ex == null)
                {
                    LogHelper.log.InfoFormat(str, objs);
                }
                else
                {
                    LogHelper.log.InfoFormat(str, objs);
                    LogHelper.log.Info(ex);
                }
            }
        }

        public static void Warn(string str)
        {
            LogHelper.Warn(str, null, null);
        }

        public static void Warn(string str, Exception ex)
        {
            LogHelper.Warn(str, ex, null);
        }

        public static void Warn(string str, params object[] objs)
        {
            LogHelper.Warn(str, null, objs);
        }

        public static void Warn(string str, Exception ex, params object[] objs)
        {
            if (LogHelper.log.IsWarnEnabled)
            {
                if (ex == null && objs == null)
                {
                    LogHelper.log.Warn(str);
                }
                else if (objs == null)
                {
                    LogHelper.log.Warn(str, ex);
                }
                else if (ex == null)
                {
                    LogHelper.log.WarnFormat(str, objs);
                }
                else
                {
                    LogHelper.log.WarnFormat(str, objs);
                    LogHelper.log.Warn(ex);
                }
            }
        }

        public static void Error(string str)
        {
            LogHelper.Error(str, null, null);
        }

        public static void Error(string str, Exception ex)
        {
            LogHelper.Error(str, ex, null);
        }

        public static void Error(string str, params object[] objs)
        {
            LogHelper.Error(str, null, objs);
        }

        public static void Error(string str, Exception ex, params object[] objs)
        {
            if (LogHelper.log.IsErrorEnabled)
            {
                if (ex == null && objs == null)
                {
                    LogHelper.log.Error(str);
                }
                else if (objs == null)
                {
                    LogHelper.log.Error(str, ex);
                }
                else if (ex == null)
                {
                    LogHelper.log.ErrorFormat(str, objs);
                }
                else
                {
                    LogHelper.log.ErrorFormat(str, objs);
                    LogHelper.log.Error(ex);
                }
            }
        }

        public static void SetLogLevel(Level level)
        {
            LogManager.GetRepository().Threshold = level;
        }

        public static bool CheckLogLevelEnable(Level level)
        {
            bool result = false;
            if (Level.Compare(level, Level.Debug) == 0)
            {
                result = LogHelper.log.IsDebugEnabled;
            }
            else if (Level.Compare(level, Level.Info) == 0)
            {
                result = LogHelper.log.IsInfoEnabled;
            }
            else if (Level.Compare(level, Level.Warn) == 0)
            {
                result = LogHelper.log.IsWarnEnabled;
            }
            else if (Level.Compare(level, Level.Error) == 0)
            {
                result = LogHelper.log.IsErrorEnabled;
            }
            return result;
        }

        public static void DebugEnter(string className, string methodName, params object[] argNameValuePairs)
        {
            if (LogHelper.log.IsDebugEnabled)
            {
                LogHelper.log.Debug(LogHelper.Enter(className, methodName, argNameValuePairs));
                System.Diagnostics.Debug.WriteLine(LogHelper.Enter(className, methodName, argNameValuePairs));
            }
        }

        public static void ErrorEnter(string className, string methodName, params object[] argNameValuePairs)
        {
            if (LogHelper.log.IsDebugEnabled)
            {
                LogHelper.log.Debug(LogHelper.Enter(className, methodName, argNameValuePairs));
            }
        }

        public static void DebugExit(string className, string methodName, params object[] resultObjects)
        {
            if (LogHelper.log.IsDebugEnabled)
            {
                LogHelper.log.Debug(LogHelper.Exit(className, methodName, resultObjects));
            }
        }

        public static void ErrorExit(string className, string methodName, params object[] resultObjects)
        {
            if (LogHelper.log.IsDebugEnabled)
            {
                LogHelper.log.Error(LogHelper.Exit(className, methodName, resultObjects));
            }
        }

        private static string Enter(string className, string methodName, object[] argNameValuePairs)
        {
            string text = string.Concat(new string[]
			{
				"开始调用",
				className,
				".",
				methodName,
				"("
			});
            if (argNameValuePairs != null)
            {
                for (int i = 0; i < argNameValuePairs.Length; i++)
                {
                    string text2 = "null";
                    if (argNameValuePairs[i] != null)
                    {
                        if (argNameValuePairs[i] is byte[])
                        {
                            text2 = StringAndBytesHelper.ToHexString(argNameValuePairs[i] as byte[], (argNameValuePairs[i] as byte[]).Length);
                        }
                        else
                        {
                            text2 = argNameValuePairs[i].ToString();
                        }
                    }
                    if (i % 2 == 0)
                    {
                        text = text + ((i == 0) ? "" : ", ") + text2 + "=";
                    }
                    else
                    {
                        text += text2;
                    }
                }
            }
            return text + ")...";
        }

        private static string Exit(string className, string methodName, object[] resultObjects)
        {
            string text = string.Concat(new string[]
			{
				"结束调用",
				className,
				".",
				methodName,
				"(). "
			});
            if (resultObjects != null && resultObjects.Length > 0)
            {
                text += "结果：";
                for (int i = 0; i < resultObjects.Length; i++)
                {
                    string text2 = "null";
                    if (resultObjects[i] != null)
                    {
                        if (resultObjects[i] is byte[])
                        {
                            text2 = StringAndBytesHelper.ToHexString(resultObjects[i] as byte[], (resultObjects[i] as byte[]).Length);
                        }
                        else
                        {
                            text2 = resultObjects[i].ToString();
                        }
                    }
                    text = text + ((i == 0) ? "" : "；") + text2;
                }
            }
            return text;
        }

        

        public static string ByteToHex(byte[] byteArray)
        {
            string text = string.Empty;
            for (int i = 0; i < byteArray.Length; i++)
            {
                byte b = byteArray[i];
                text += b.ToString("X2");
            }
            return text;
        }
    }
}
